package com.agv.web.adm.sec;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.agv.core.services.ISecurityService;
import com.agv.core.util.Constantes;
import com.agv.web.MonitoringDispatchAction;

public class AdmPerfilDispatchAction extends MonitoringDispatchAction {

	private ISecurityService securityService;

	public ActionForward insertarBd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			AdmPerfilForm admPerfilForm = (AdmPerfilForm) form;

			if (admPerfilForm.getNbPerfil().equals("")) {
				ActionMessages messages = new ActionMessages();
				ActionMessage msg = new ActionMessage(
						"agv.adm.cat.campoRequerido");
				messages.add("message", msg);
				saveMessages(request, messages);
				return mapping.findForward("cfgPerfiles");
			} else if (admPerfilForm.getIdFuncion() == null) {
				ActionMessages messages = new ActionMessages();
				ActionMessage msg = new ActionMessage(
						"agv.adm.cat.seleccionarPerfil");
				messages.add("message", msg);
				saveMessages(request, messages);
				return mapping.findForward("cfgPerfiles");
			}

			securityService.insertaPerfil(admPerfilForm.getNbPerfil(), Integer
					.parseInt(admPerfilForm.getStPerfil()), admPerfilForm
					.getIdFuncion());
			request.getSession().setAttribute("catInsertado", "si");
			return mapping.findForward("cfgPerfiles");
		} catch (Exception e) {
			monitoringError(request, e);
			log.error(e, e);
			return mapping.findForward(Constantes.FWD_ERR);
		}
	}

	public ActionForward borrar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("cfgPerfiles");
	}

	public ActionForward actualizar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("cfgPerfiles");
	}

	/**
	 * @param securityService
	 *            the securityService to set
	 */
	public void setSecurityService(ISecurityService securityService) {
		this.securityService = securityService;
	}

}
